package com.lu.stack.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * @author zhanglu
 * @project lu-cloud-stack
 * @description 路由配置
 * @date 2023/2/6 15:48
 */
@Slf4j
@Configuration
public class RouteConfig {

    /**
     * 对请求基于ip的访问进行限流
     *
     * @return
     */
    @Primary
    @Bean("ipKeyResolver")
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }

    /**
     * 根据uri限流
     *
     * @return
     */
    @Bean("uriKeyResolver")
    public KeyResolver uriKeyResolver() {
        return (exchange) -> {
            return Mono.just(exchange.getRequest().getURI().getPath());
        };
    }

}
